package com.gaoyi.basemodule.baseUtil;

import android.content.Context;
import android.content.SharedPreferences;


public class SPUtil {
    private static volatile SPUtil instance;
    private final SharedPreferences share;
    private final SharedPreferences.Editor editor;

    public SPUtil() {
        share = AppUtil.getContext().getSharedPreferences("synergos", Context.MODE_PRIVATE);
        editor = share.edit();
    }

    public static SPUtil getInstance() {
        if (instance == null) {
            synchronized (SPUtil.class) {
                if (instance == null) {
                    instance = new SPUtil();
                }
            }
        }
        return instance;
    }

    public void putString(String spName, String value) {
        editor.putString(spName, value);
        editor.apply();
    }

    public String getString(String spName) {
        return share.getString(spName, "");
    }

    public String getString(String spName,String defaultValue) {
        return share.getString(spName, defaultValue);
    }

    public void putInt(String spName, int value) {
        editor.putInt(spName, value);
        editor.apply();
    }

    public int getInt(String spName, int defaultValue) {
        return share.getInt(spName, defaultValue);
    }

    public void putBoolean(String spName,boolean value){
        editor.putBoolean(spName,value);
        editor.apply();
    }

    public boolean getBoolean(String spName,boolean defaultValue){
        return share.getBoolean(spName,defaultValue);
    }

    /**
     * 切换语言时，其他进程要重新获取SharedPreferences
     */
    public void refresh() {
        instance = null;
    }
}
